<?php
    header("content-type:text/html;charset=utf-8");
    function shangchuan($file,$filename = 'images',$maxSize = 210241024,$allow = array('png','jpg','gif')){
        $name = $file['name'];
        $type = $file['type'];
        $tmp_name = $file['tmp_name'];
        $error = $file['error'];
        $size = $file['size'];
        if($error == 0){
            $ext = @end(explode('.',$name));
            $picname = md5(time());
            if($size > $maxSize){
                die('上传文件超过了限定大小');
            }
            if(!in_array($ext,$allow)){
                die('上传文件类型不符合');
            }
            if(!is_uploaded_file($tmp_name)){
                die('非http post方式上传');
            }
            if(!getimagesize($tmp_name)){
                die('非真实图片');
            }
            if(!file_exists($filename)){
                mkdir($filename);
            }
            $des = $filename.'/'.$picname.'.'.$ext;
            if(move_uploaded_file($tmp_name, $des)){
                return $picname.'.'.$ext;
                echo "<script> alert('上传成功'); </script>";
            }else{
                die('上传失败');
            }
        }else{
            switch ($error) {
                case 1:
                    return '上传文件超过了php.ini中upload_max_filesize设置的值';
                    break;
                case 2:
                    return '上传文件的大小超过了html表单中MAX_FILE_SIZE指定的值';
                    break;
                case 3:
                    return '文件只有部分被上传';
                    break;
                case 4:
                    return '没有文件被上传';
                    break;
                case 6:
                    return '找不到临时文件夹';
                    break;
                case 7:
                    return '文件写入失败';
                    break;
                case 8:
                    return '上传被php拓展程序中断';
                    break;
            }
        }
    }
?>